package org.example.component;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.StringWriter;

/**
 * @author wangfei
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class FreeMakerHelper {
    private final Configuration freeMarkerConfiguration;

    public String process(String path, Object modelData) throws TemplateException, IOException {
        Template template = freeMarkerConfiguration.getTemplate(path);
        StringWriter out = new StringWriter();
        template.process(modelData, out);
        return out.toString();
    }
}
